namespace Launcher.View

import System
import System.Drawing

import Launcher.Model

callable RankingItemChangedHandler(item as RankingViewItem)
callable RankingItemSizeChangedHandler(item as RankingViewItem)

class RankingViewItem:
"""Description of RankingViewItem"""

	event RankingItemChanged as RankingItemChangedHandler
	event RankingItemSizeChanged as RankingItemSizeChangedHandler
	
	[getter(Model)]
	_appInfo as ApplicationInfo
	
	[property(X)]
	_x as int
	
	[property(Y)]
	_y as int
		
	[property(DrawSize)]
	_dsize as int
	
	Size as int:
		get:
			return _size
			
		set:
			_size = value
			if _dsize == 0: _dsize = value
			
	_size as int
	
	TooltipText as string:
		get:
			if not string.IsNullOrEmpty(_appInfo.Description.Trim()):
				return _appInfo.Description
				
			return "${_appInfo.Path} ${_appInfo.Arguments}"

	def constructor(appInfo as ApplicationInfo):
		_appInfo = appInfo
		_appInfo.RankingChanged += _OnRankingChanged
		
	private def _OnRankingChanged():
		RankingItemChanged(self)

	def MouseMove(x as int, y as int):
		UpdateDrawSize(x, y)
		
	private def UpdateDrawSize(x as int, y as int):
		d = distance(x, y, _x, _y)
		oldSize = _dsize
		
		if d > _size:
			_dsize = _size
		else:
			_dsize = _size + _size - d
			
		if _dsize != oldSize: RankingItemSizeChanged(self)
			
	private def distance(x1 as int, y1 as int, x2 as int, y2 as int):
		dx = x1 - x2
		dy = y1 - y2
		
		return cast(int, Math.Sqrt(dx**2 + dy**2))

	def DrawItem(g as Graphics):
		x = X - DrawSize / 2
		y = Y - DrawSize / 2
		g.DrawIcon(Model.Icon, Rectangle(x, y, DrawSize, DrawSize))

	def Contains(x as int, y as int):
		r = Rectangle(X - DrawSize / 2, Y - DrawSize / 2, DrawSize, DrawSize)
		return r.Contains(x, y)

	def ResetDrawSize():
		_dsize = _size
